package de.fhd.pms.dao;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import de.fhd.pms.model.User;

/** The data access class for Users. */
public class UserDao extends HibernateDaoSupport {


	/**
	 * <p>Finds a {@link User} by specifying the userId.</p>
	 * @param userId
	 * @return object {@link User}
	 */
	public User findByUserId(int userId) {
		HibernateTemplate template = getHibernateTemplate();
		return (User) template.get(User.class, userId);
	}

	/**
	 * <p>Find a {@link User} by specifying the parameter in the database. </p>
	 * @param {@link user}
	 * @return {@link user}
	 */
	public String findByUser(User user) {
		HibernateTemplate template = getHibernateTemplate();
		return template.get(User.class, user).getUsername();
	}
	
	/**
	 * <p>Searches the database for all Users and returns them.</p>
	 * @return A list of users
	 */
	public List<User> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		return template.loadAll(User.class);
	}
	
	/**
	 * <p>Find a {@link User} by specifying its Username.</p>
	 * @param username
	 * @return object {@link user}
	 */
	public User findByUsername(String username) {

		HibernateTemplate template = getHibernateTemplate();
		// search user in the database
		// if found more then one user with the specified username
		// then return first user object in the list
		try
        {
            List<User> tmp = template.find(
                    "from users as pu where u.username = ?",
                    username
            );

            if (tmp != null)
                return (User)tmp.get( 0 );
            else
                return null;
        }
        catch( Throwable t )
        {
        }
		// load all users as objects from the datav=base
		List<User> userList = template.loadAll(User.class);
		// get amount of users
		int userListLength = userList.size();
		// initialise user as null
		User user = null;
		// set user id to corrupted value
		Integer user_Id = -1;
		/// search in the list with users for user with specified
		// in the parameter username
		// the first found user with specified username is returned as result
		for (int i = 0; i < userListLength; i++) {
			user = userList.get(i);
			if (user.getUsername().equals(username)) {
				user_Id = user.getId();
				return user;
			}
		}

		return user;
	}
	
	/**
	 * Creates and saves a {@link User} object into the database.
	 * @param user
	 * @return object {@link user}
	 */
	public User save(User user) {
		// set that new user by default is allowed to login
		user.setEnabled(1);
		HibernateTemplate template = getHibernateTemplate();
		template.saveOrUpdate(user);
		return user;
	}
	
	/**
	 * Deletes a {@link User} by specifying the parameter in the database.
	 * @param user
	 */
	public boolean delete(User user){
		HibernateTemplate template = getHibernateTemplate();
		try {
			template.delete(user);
			return true;
		}
		catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}

}
